/*
Copyright (c) 2010 Bill Davidsen (wdavidsen@yahoo.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
using System;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.XPath;

namespace SCS.Web.UI.WebControls
{
	public enum ExpandDirectionType { Right, Left }

	/// <summary>
	/// Summary description for MultiMenuGroup.
	/// </summary>
	/// 
	public class MultiMenuGroup
	{		
		#region Serialization Mappings

		string[,] serializationMap = new  string[,] {	{"MenuGroup", "MG"}, 
														{"Width", "W"}, 
														{"ExpandDirection", "ED"},
														{"CssClass", "C"},
														{"VerticalOffset", "VO"},
														{"HorizontalOffset", "HO"}
													};
		#endregion

		#region Fields
		
		private MultiMenuItems _menuItems = null;
		private MultiMenuItem _parentMenuItem = null;

		private string cssClass = string.Empty;
		private string id = string.Empty;
		
		private int width = -1;
		private int verticalOffset = 0;
		private int horizontalOffset = 0;
		
		private ExpandDirectionType _expandDirectionType = ExpandDirectionType.Right;
        private MenuMarker _rightMenuMarker = null;
        private MenuMarker _leftMenuMarker = null;
		#endregion

		#region Constructors
		internal MultiMenuGroup()
		{
			this._menuItems = new MultiMenuItems();
			this._menuItems.ParentMenuGroup = this;
		}

		internal MultiMenuGroup(ExpandDirectionType expandDirection)
		{
			this._menuItems = new MultiMenuItems();
			this._menuItems.ParentMenuGroup = this;

			this.ExpandDirection = expandDirection;
		}

		internal MultiMenuGroup(ExpandDirectionType expandDirection, int width)
		{
			this._menuItems = new MultiMenuItems();
			this._menuItems.ParentMenuGroup = this;

			this.ExpandDirection = expandDirection;
			this.Width = width;
		}

		internal MultiMenuGroup(ExpandDirectionType expandDirection, int width, int horizontalOffset, int verticalOffset)
		{
			this._menuItems = new MultiMenuItems();
			this._menuItems.ParentMenuGroup = this;

			this.ExpandDirection = expandDirection;
			this.Width = width;
			this.HorizontalOffset = horizontalOffset;
			this.VerticalOffset = verticalOffset;
		}
		#endregion

		private string getNodeText(string shortName, bool compress)
		{
			if (compress)
				return shortName;

			for (int i = 0; i < this.serializationMap.GetLength(0); i++)
			{
				if (this.serializationMap[i, 1].Equals(shortName))
					return this.serializationMap[i, 0];
			}
			return shortName;
		}		
		internal void Serialize(XmlTextWriter writer, bool compress)
		{
			writer.WriteStartElement(this.getNodeText("MG", compress));

			if (this.Width > -1)
				writer.WriteAttributeString(this.getNodeText("W", compress), this.Width.ToString());

			if (this.ExpandDirection == ExpandDirectionType.Right)
				writer.WriteAttributeString(this.getNodeText("ED", compress), "Right");
			else
				writer.WriteAttributeString(this.getNodeText("ED", compress), "Left");

			if (this.CssClass.Length > 0)
				writer.WriteAttributeString(this.getNodeText("C", compress), this.CssClass);

			if (this.VerticalOffset != 0)
				writer.WriteAttributeString(this.getNodeText("VO", compress), this.VerticalOffset.ToString());

			if (this.HorizontalOffset != 0)
				writer.WriteAttributeString(this.getNodeText("HO", compress), this.HorizontalOffset.ToString());
		}
		
		internal void DeserializeMenuGroup(MultiMenuGroup menuGroup, XPathNavigator navigator, bool isCompressed)
		{
			navigator.MoveToFirstAttribute();
			this.setProperty(menuGroup, navigator, isCompressed);

			while (navigator.MoveToNextAttribute())
				this.setProperty(menuGroup, navigator, isCompressed);
		}		
		private void setProperty(MultiMenuGroup menuGroup, XPathNavigator navigator, bool isCompressed)
		{
			try
			{
				if (navigator.Name.Equals(this.getNodeText("W", isCompressed)))
					menuGroup.Width = int.Parse(navigator.Value);

				if (navigator.Name.Equals(this.getNodeText("C", isCompressed)))
					menuGroup.CssClass = navigator.Value;

				if (navigator.Name.Equals(this.getNodeText("ED", isCompressed)))
				{
					if (navigator.Value == "Right")
						menuGroup.ExpandDirection = ExpandDirectionType.Right;
					else
						menuGroup.ExpandDirection = ExpandDirectionType.Left;
				}

				if (navigator.Name.Equals(this.getNodeText("VO", isCompressed)))
					menuGroup.VerticalOffset = int.Parse(navigator.Value);

				if (navigator.Name.Equals(this.getNodeText("HO", isCompressed)))
					menuGroup.HorizontalOffset = int.Parse(navigator.Value);
			}
			catch (Exception e)
			{
				string message = string.Format("Unable to set {0} to \"{1}\" due to type mismatch.", navigator.Name, navigator.Value);
				throw new ApplicationException(message, e);
			}
		}
				
		#region Properties

		public MultiMenuItem ParentMenuItem
		{
			get { return this._parentMenuItem; }
			set { this._parentMenuItem = value; }
		}

		internal string ID_internal
		{
			get { return this.id; }
			set { this.id = value; }
		}
		public string Id
		{
			get { return this.id; }
		}
		public string CssClass
		{
			get { return this.cssClass; }
			set { this.cssClass = value; }
		}
		public int Width
		{
			get { return this.width; }
			set { this.width = value; }
		}
		public int VerticalOffset
		{
			get { return this.verticalOffset; }
			set { this.verticalOffset = value; }
		}
		public int HorizontalOffset
		{
			get { return this.horizontalOffset; }
			set { this.horizontalOffset = value; }
		}
		public MultiMenuItems MultiMenuItems
		{
			get { return this._menuItems; }
			set { this._menuItems = value; }
		}
		public ExpandDirectionType ExpandDirection
		{
			get { return this._expandDirectionType; }
			set { this._expandDirectionType = value; }
		}
        public MenuMarker RightMenuMarkerForItemsToUse
        {
            get { return this._rightMenuMarker; }
            set { this._rightMenuMarker = value; }
        }
        public MenuMarker LeftMenuMarkerForItemsToUse
        {
            get { return this._leftMenuMarker; }
            set { this._leftMenuMarker = value; }
        }

		#endregion
	}
}
